package com.spgf.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.dao.util.PadraoDAOAb;
import com.spgf.model.entities.CategoriaModel;
import com.spgf.model.entities.OrcamentoModel;
import com.spgf.model.entities.utils.ModelAb;
import com.spgf.model.utils.AndroidUtil;

public class OrcamentoDAO extends PadraoDAOAb {

	public final static String	COLUNA_CATEGORIA	= "CD_CATEGORIA";

	public final static String	COLUNA_CODIGO		= "CD_ORCAMENTO";
	public final static String	COLUNA_DATA_FIM		= "DT_FIM";
	public final static String	COLUNA_DATA_INICIO	= "DT_INICIO";
	public final static String	COLUNA_VALOR		= "VL_ORCAMENTO";
	public final static String	NOME_TABELA			= "TBORCAMENTO";

	public OrcamentoDAO(Context context) {
		super(context);
	}

	@Override
	public <Model extends ModelAb> String getClausuraWherePadrao(Model pModel) throws NegocioException {

		OrcamentoModel lModel = (OrcamentoModel) pModel;

		StringBuilder lSql = new StringBuilder();
		lSql.append(" 1=1 ");

		if (lModel.getCategoria() != null && lModel.getCategoria().getCodigo() != null) {
			lSql.append(" AND ");
			lSql.append(COLUNA_CATEGORIA);
			lSql.append(" = ");
			lSql.append(lModel.getCategoria().getCodigo());
		}

		return lSql.toString();
	}

	@Override
	protected String[] getColuns() {
		return new String[] {COLUNA_CODIGO, COLUNA_DATA_INICIO, COLUNA_DATA_FIM, COLUNA_VALOR, COLUNA_CATEGORIA};
	}

	@Override
	@SuppressWarnings("unchecked")
	public <Model extends ModelAb> Model getDependencias(Model pModel) throws NegocioException {

		OrcamentoModel lModel = (OrcamentoModel) pModel;
		lModel.setCategoria(new CategoriaDAO(getContext()).obterPorCodigo(new CategoriaModel(lModel.getCategoria().getCodigo())));
		return (Model) lModel;
	}

	@Override
	@SuppressWarnings("unchecked")
	protected <Model extends ModelAb> Model getModel(Cursor cursor) throws NegocioException {

		final OrcamentoModel newModel = new OrcamentoModel();
		newModel.setCodigo(cursor.getInt(cursor.getColumnIndex(COLUNA_CODIGO)));
		newModel.setDataInicial(AndroidUtil.toDate(cursor.getString(cursor.getColumnIndex(COLUNA_DATA_INICIO))));
		newModel.setDataFinal(AndroidUtil.toDate(cursor.getString(cursor.getColumnIndex(COLUNA_DATA_FIM))));
		newModel.setCategoria(new CategoriaModel(cursor.getInt(cursor.getColumnIndex(COLUNA_CATEGORIA))));
		newModel.setValor(cursor.getDouble(cursor.getColumnIndex(COLUNA_VALOR)));
		return (Model) newModel;
	}

	@Override
	protected String getNomeColunaCodigo() {
		return COLUNA_CODIGO;
	}

	@Override
	protected String getNomeTabela() {
		return NOME_TABELA;
	}

	@Override
	protected <Model extends ModelAb> ContentValues getValues(Model pModel) throws NegocioException {

		OrcamentoModel lModel = (OrcamentoModel) pModel;

		final ContentValues values = new ContentValues();
		values.put(COLUNA_DATA_INICIO, AndroidUtil.toStringSQLite(lModel.getDataInicial()));
		values.put(COLUNA_DATA_FIM, AndroidUtil.toStringSQLite(lModel.getDataFinal()));
		values.put(COLUNA_CATEGORIA, lModel.getCategoria().getCodigo());
		values.put(COLUNA_VALOR, lModel.getValor());
		return values;
	}
}
